/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatroom.com.chatEvent;

import chatroom.com.entity.FontAttrib;
import chatroom.com.entity.MessageModel;
import chatroom.com.util.DateUtil;
import java.awt.Color;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.Calendar;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class Chat {

    /**
     * 功能：将表情插入到JTextPane中
     *
     * @param args
     */
    public static void insertpic(JTextPane p, ImageIcon pic) {
        Document doc = p.getStyledDocument();
        p.insertIcon(pic);

    }

    /**
     * 功能：退出聊天室
     *
     * @param out
     */
    public static void shutDown(PrintWriter out) {
        try {
            out.println("quit");
            out.flush();
        } catch (Exception ee) {
            ee.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    /**
     * 功能：发送聊天信息
     *
     * @param oos
     * @param out
     * @param msg
     */
    public static void sendMessage(PrintWriter out, MessageModel msg,
            JTextArea jta) {
        try {
            String mywords = jta.getText();
            msg.setContent(ChatFilter.getAfterFilterString(jta.getText()));
            if (mywords.length() == 0) {
                JOptionPane.showMessageDialog(null, "您发送的消息为空，请重新输入");
            }
            if ((mywords.trim()).length() != 0)// 不能发送空消息也不能都发空格
            {
                out.println("usermessage&" + MessageModel.getStringByMessage(msg));
                out.flush();
                System.out.println(msg.toString());

            }
        } catch (Exception ee) {
            ee.printStackTrace();
        } finally {
            jta.setText(""); // 清空输入框
        }

    }

    /**
     * 功能：将文本插入JTextPane
     *
     * @param attrib
     */
    public static void sysXiaoxi(JTextPane p, String text, Color color, int size)// 根据传入的颜色及文字，将文字插入文本域
    {
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set, color);// 设置文字颜色
        StyleConstants.setFontSize(set, size);// 设置字体大小

        Document doc = p.getStyledDocument();
        int h, m, s;
        Calendar cal = Calendar.getInstance();
        h = cal.get(Calendar.HOUR_OF_DAY);
        m = cal.get(Calendar.MINUTE);
        s = cal.get(Calendar.SECOND);
        try {
            doc.insertString(doc.getLength(), h + ":" + m + ":" + s + " "
                    + text + "\n", set);// 插入文字
        } catch (BadLocationException e) {
        }
    }

    /**
     * 功能：获得字体的相关设置
     *
     * @param message
     * @param font
     * @param size
     * @param stytle
     * @param color
     * @return
     */
    public static FontAttrib getFontAttrib(JTextArea message, MessageModel msg) {
        FontAttrib att = new FontAttrib();
        att.setText(message.getText());
        att.setName(msg.getZiti());
        att.setSize(Integer.parseInt(msg.getDaxiao()));
        String temp_style = msg.getYangshi();
        if (temp_style.equals("常规")) {
            att.setStyle(FontAttrib.GENERAL);
        } else if (temp_style.equals("粗体")) {
            att.setStyle(FontAttrib.BOLD);
        } else if (temp_style.equals("斜体")) {
            att.setStyle(FontAttrib.ITALIC);
        } else if (temp_style.equals("粗斜体")) {
            att.setStyle(FontAttrib.BOLD_ITALIC);
        }
        String temp_color = msg.getYanse();
        if (temp_color.equals("黑色")) {
            att.setColor(new Color(0, 0, 0));
        } else if (temp_color.equals("红色")) {
            att.setColor(new Color(255, 0, 0));
        } else if (temp_color.equals("蓝色")) {
            att.setColor(new Color(0, 0, 255));
        } else if (temp_color.equals("黄色")) {
            att.setColor(new Color(255, 255, 0));
        } else if (temp_color.equals("绿色")) {
            att.setColor(new Color(0, 255, 0));
        }

        return att;
    }

    /**
     * 功能：获得字体的相关设置
     *
     * @param message
     * @param font
     * @param size
     * @param stytle
     * @param color
     * @return
     */
    public static FontAttrib getSystemFontAttrib(JTextArea message) {
        FontAttrib att = new FontAttrib();
        att.setText(message.getText());
        att.setName("宋体");
        att.setSize(13);
        String temp_style = "粗体";
        if (temp_style.equals("常规")) {
            att.setStyle(FontAttrib.GENERAL);
        } else if (temp_style.equals("粗体")) {
            att.setStyle(FontAttrib.BOLD);
        } else if (temp_style.equals("斜体")) {
            att.setStyle(FontAttrib.ITALIC);
        } else if (temp_style.equals("粗斜体")) {
            att.setStyle(FontAttrib.BOLD_ITALIC);
        }
        String temp_color = "黑色";
        if (temp_color.equals("黑色")) {
            att.setColor(new Color(0, 0, 0));
        } else if (temp_color.equals("红色")) {
            att.setColor(new Color(255, 0, 0));
        } else if (temp_color.equals("蓝色")) {
            att.setColor(new Color(0, 0, 255));
        } else if (temp_color.equals("黄色")) {
            att.setColor(new Color(255, 255, 0));
        } else if (temp_color.equals("绿色")) {
            att.setColor(new Color(0, 255, 0));
        }

        return att;
    }

    /**
     * 功能：将聊天信息插入到公共聊天面板
     *
     * @param p
     * @param msg
     * @param jta
     */
    public static String inserts(JTextPane p, String strs, MessageModel msg,
            JTextArea jta) {
        String fileStr = "";
        Document doc = p.getStyledDocument();
        try {

            doc.insertString(doc.getLength(), DateUtil.getCurrentTimestamps() + "  ", getSystemFontAttrib(jta).getAttrSet());// AAAA系统自带的一些信息
            fileStr = fileStr + DateUtil.getCurrentTimestamps() + "  ";
        } catch (Exception e) {
            System.out.println("a big error here");
        }
        try {

            doc.insertString(doc.getLength(), strs, getSystemFontAttrib(jta).getAttrSet());// AAAA系统自带的一些信息
            fileStr = fileStr + strs;
        } catch (Exception e) {
            System.out.println("a big error here");
        }
        String s = msg.getContent();
        int length = s.length();// 获取String 长度
        char[] every = new char[length];
        int count = 0;// 初始字符的位置，变化

        Boolean hadjin = false;
        for (int i = 0; i < length; i++) {
            every[i] = s.charAt(i);
            if (every[i] == '#') // 识别信息中是否有#号
            {
                hadjin = true;
            }
        }
        for (int i = 0; i < length; i++) {
            every[i] = s.charAt(i);

            if (hadjin = false) {
                break;
            }
            if (every[i] == '#') {
                String str = null;
                str = s.substring(count, i); // 得到表情前的文字

                try {
                    if (str != null) {
                        doc.insertString(doc.getLength(), str,
                                getFontAttrib(jta, msg).getAttrSet());// AAAA添加表情前的文字
                    }
                    fileStr = fileStr + str;
                } catch (Exception e) {
                    System.out.println("a big error here");
                }

                String icName;
                icName = s.substring(i + 1, i + 3); // 得到表情的名字 #01
                System.out.println("表情的名字是：" + icName);
                String pics = icName + ".gif";
                System.out.println("表情的文件名称为：" + pics);
                // Icon ic=new ImageIcon(pics);//将表情转化为icon
                p.setCaretPosition(doc.getLength());// 获取当前的位置，将表情插入到当前最下~~~~

                // 没有的话 此表情将出现在JTextPane的最上方，无法出现在当前行
                p.insertIcon(new ImageIcon("src\\chatroom\\resource\\" + pics));
                count = i + 3;// 将字符起始位置跳到表情后第一位置
                if (count == length) {
                    try {
                        doc.insertString(doc.getLength(), "",
                                getFontAttrib(jta, msg).getAttrSet());
                    } catch (Exception ee) {
                    }
                }
                System.out.println("图片在的位置是：" + count);
            }
        }

        if (count < length) {
            String theLast = null;
            theLast = s.substring(count, length);
            try {

                doc.insertString(doc.getLength(), theLast + "\n",
                        getFontAttrib(jta, msg).getAttrSet());
                fileStr = fileStr + theLast + "\n";
            } catch (Exception e) {
                System.out.println("a big error here");
            }
        } else {
            try {

                doc.insertString(doc.getLength(), "\n",
                        getFontAttrib(jta, msg).getAttrSet());

            } catch (Exception e) {
                System.out.println("a big error here");
            }
        }
        System.err.println("插入本地文件中的字符串为：" + fileStr);
        return fileStr;

    }

    /**
     * 功能：将系统信息插入到聊天面板
     *
     * @param j
     * @param jta
     * @param msg
     * @param doc
     */
    public static void insert(JTextPane j, JTextArea jta, MessageModel msg,
            Document doc) {
        doc = j.getStyledDocument();
        try { // 插入文本
            // doc.insertString(doc.getLength(), h + ":" + m + ":" + s +
            // " "+ words + "\n", getFontAttrib().getAttrSet());
            doc.insertString(doc.getLength(), msg.getContent() + "\n",
                    getFontAttrib(jta, msg).getAttrSet());
            jta.setText("");

        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能：将文本插入JTextPane
     *
     * @param attrib
     */
    public static void sysPutongXiaoxi(JTextPane p, String text)// 根据传入的颜色及文字，将文字插入文本域
    {
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set, Color.red);// 设置文字颜色
        StyleConstants.setFontSize(set, 12);// 设置字体大小
        Document doc = p.getStyledDocument();
        int h, m, s;
        Calendar cal = Calendar.getInstance();
        h = cal.get(Calendar.HOUR_OF_DAY);
        m = cal.get(Calendar.MINUTE);
        s = cal.get(Calendar.SECOND);
        try {
            doc.insertString(doc.getLength(), h + ":" + m + ":" + s + " "
                    + text + "\n", set);// 插入文字
        } catch (BadLocationException e) {
        }
    }

    public static void insertIntoHistoryPane(JTextPane p, String text)// 根据传入的颜色及文字，将文字插入文本域
    {
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set, Color.BLACK);// 设置文字颜色
        StyleConstants.setFontSize(set, 12);// 设置字体大小
        Document doc = p.getStyledDocument();

        try {
            doc.insertString(doc.getLength(), text + "\n", set);// 插入文字
        } catch (BadLocationException e) {
        }
    }
}
